package com.industrial.premu.controller;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.core.domain.R;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuJszxHy;
import com.industrial.premu.domain.PreMuStandardIndustryInfo;
import com.industrial.system.api.domain.SysUser;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuCompanyInfo;
import com.industrial.premu.service.IPreMuCompanyInfoService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 企业基本信息Controller
 *
 * @author SangGuangCan
 * @date 2024-01-24
 */
@RestController
@RequestMapping("/compny_info")
public class PreMuCompanyInfoController extends BaseController
{
    @Autowired
    private IPreMuCompanyInfoService preMuCompanyInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/selectEnterCompanyId/{uscc}")
    public R<String> selectEnterCompanyId(@PathVariable("uscc") String uscc){
        String companyId = preMuCompanyInfoService.selectEnterCompanyId(uscc);
        return R.ok(companyId);
    }

    /**
     * 查询企业基本信息列表
     */
    @RequiresPermissions("premu:compny_info:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuCompanyInfo preMuCompanyInfo)
    {
        startPage();
        List<PreMuCompanyInfo> list = preMuCompanyInfoService.selectPreMuCompanyInfoList(preMuCompanyInfo);
        return getDataTable(list);
    }

    /**
     * 导出企业基本信息列表
     */
    @RequiresPermissions("premu:compny_info:export")
    @Log(title = "企业基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuCompanyInfo preMuCompanyInfo)
    {
        List<PreMuCompanyInfo> list = preMuCompanyInfoService.selectPreMuCompanyInfoList(preMuCompanyInfo);
        ExcelUtil<PreMuCompanyInfo> util = new ExcelUtil<PreMuCompanyInfo>(PreMuCompanyInfo.class);
        util.exportExcel(response, list, "企业基本信息数据");
    }

    /**
     * 获取企业基本信息详细信息
     */
    @RequiresPermissions("premu:compny_info:query")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(PreMuCompanyInfo preMuCompanyInfo)
    {
        return preMuCompanyInfoService.selectPreMuCompanyInfoById(preMuCompanyInfo);
    }

    /**
     * 新增企业基本信息
     */
    @RequiresPermissions("premu:compny_info:add")
    @Log(title = "企业基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuCompanyInfo preMuCompanyInfo)
    {
        return toAjax(preMuCompanyInfoService.insertPreMuCompanyInfo(preMuCompanyInfo));
    }

    /**
     * 修改企业基本信息
     */
    @RequiresPermissions("premu:compny_info:edit")
    @Log(title = "企业基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuCompanyInfo preMuCompanyInfo)
    {
        return toAjax(preMuCompanyInfoService.updatePreMuCompanyInfo(preMuCompanyInfo));
    }

    /**
     * 删除企业基本信息
     */
    @RequiresPermissions("premu:compny_info:remove")
    @Log(title = "企业基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(preMuCompanyInfoService.deletePreMuCompanyInfoByIds(ids));
    }

    /**
     * 导入企业数据
     * @param file
     * @return
     */
    @RequiresPermissions("premu:compny_info:importenterinfo")
    @PostMapping("/importEnterInfo")
    public AjaxResult importEnterInfo(@RequestParam Map<String,String> prams, @RequestPart("file")MultipartFile file, HttpServletRequest request){
        String requestURL = request.getRequestURL().toString();
        prams.put("url", requestURL);
        return preMuCompanyInfoService.importEnterInfo(prams,file);
    }

    /**
     * 删除企业信息
     * @param params
     * @return
     */
    @RequiresPermissions("premu:compny_info:deletecompanyinfo")
    @DeleteMapping("/deleteCompanyInfo")
    public AjaxResult deleteCompanyInfo(@RequestBody Map<String,String> params){
        return preMuCompanyInfoService.deleteCompanyInfo(params);
    }

    /**
     * 删除不参评企业
     * @return
     */
    @RequiresPermissions("premu:compny_info:deletecompanyinfono")
    @DeleteMapping("/deleteCompanyInfoNo")
    public AjaxResult deleteCompanyInfoNo(@RequestBody Map<String,String> params){
        return preMuCompanyInfoService.deleteCompanyInfoNo(params);
    }

    /**
     * 删除参评企业
     * @return
     */
    @RequiresPermissions("premu:compny_info:deletecompanyinfocp")
    @DeleteMapping("/deleteCompanyInfoCp")
    public AjaxResult deleteCompanyInfoCp(@RequestBody Map<String,String> params){
        return preMuCompanyInfoService.deleteCompanyInfoCp(params);
    }

    /**
     * 删除模块
     * @param params
     * @return
     */
    @RequiresPermissions("premu:compny_info:deletemode")
    @DeleteMapping("/deleteMode")
    public AjaxResult deleteMode(@RequestBody Map<String,String> params){
        return preMuCompanyInfoService.deleteMode(params);
    }

    @RequiresPermissions("premu:compny_info:resetpassword")
    @PostMapping("/resetPassword")
    public AjaxResult resetPassword(@RequestBody Map<String,String> params){
        return preMuCompanyInfoService.resetPassword(params);
    }

    @GetMapping("/queryOldCompanyInfo")
    public AjaxResult queryOldCompanyInfo(){
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        //userName为该企业的统一社会信用代码
        String userName = sysUser.getUserName();
        PreMuCompanyInfo companyInfo = preMuCompanyInfoService.queryOldCompanyInfo(userName);
        return AjaxResult.success(companyInfo);
    }

    /**
     * 查询行业大类数据
     * @return
     */
    @GetMapping("/queryIndustryDlList")
    public AjaxResult queryIndustryDlList(){
        List<PreMuJszxHy> industryList = (List<PreMuJszxHy>) redisTemplate.opsForValue().get("allIndustry");
        if(industryList == null){
            synchronized (this){
                industryList = (List<PreMuJszxHy>) redisTemplate.opsForValue().get("allIndustry");
                if (industryList == null){
                    //查询数据库
                    industryList = preMuCompanyInfoService.getJszx_hy();
                    //将查出来的数据放到redis缓存中
                    redisTemplate.opsForValue().set("addIndustry",industryList, DateUtils.getSeconds(), TimeUnit.SECONDS);
                }
            }
        }
        return AjaxResult.success(industryList);
    }

    /**
     * 查询行业大类中的中类
     * @param dl
     * @return
     */
    @GetMapping("/queryDlInZl/{dl}")
    public AjaxResult queryDlInZl(@PathVariable("dl") String dl){
        List<PreMuStandardIndustryInfo> standarList = preMuCompanyInfoService.getStandard(dl);
        return AjaxResult.success(standarList);
    }

    /**
     * 园区
     * @param preMuCompanyInfo
     * @return
     */
    @GetMapping(value = "/zoneList")
    public AjaxResult zoneList(PreMuCompanyInfo preMuCompanyInfo,@RequestParam(name = "calFlag", required = false) String calFlag)
    {
        return preMuCompanyInfoService.zoneList(preMuCompanyInfo, calFlag);
    }

    /**
     * 行业
     * @param preMuCompanyInfo
     * @return
     */
    @GetMapping(value = "/industryList")
    public AjaxResult industryList(PreMuCompanyInfo preMuCompanyInfo,@RequestParam(name = "calFlag", required = false) String calFlag)
    {
        return preMuCompanyInfoService.industryList(preMuCompanyInfo, calFlag);
    }
}
